]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/InputController.cs
Moves to ActorManager arch + Actor Inherited stuff
[rbdr/super-polarity] / Super Polarity / InputController.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework.Input;
6
7 namespace SuperPolarity
8 {
9 static class InputController
10 {
11 static Dictionary<string, List<Action<float>>> Listeners;
12 static Dictionary<string, List<Keys>> RegisteredKeys;
13 static Dictionary<string, List<Buttons>> RegisteredButtons;
14
15 static GamePadState InputGamePadState;
16 static KeyboardState InputKeyboardState;
17
18 /*
19 * Registered Events.
20 *
21 * You register: name of the event (ie. attack) and a key associated with it.
22 * or button... Left Stick /always/ dispatches move event.
23 */
24
25 static InputController()
26 {
27 Listeners = new Dictionary<string,List<Action<float>>>();
28 InputKeyboardState = new KeyboardState();
29 InputGamePadState = new GamePadState();
30 }
31
32 public static void UpdateInput()
33 {
34 Poll();
35 DispatchMoveEvents();
36 DispatchRegisteredEvents();
37 }
38
39 private static void Poll()
40 {
41 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
42 InputKeyboardState = Keyboard.GetState();
43 }
44
45 private static void DispatchRegisteredEvents()
46 {
47 }
48
49 private static void DispatchMoveEvents()
50 {
51 float xMovement = 0.0f;
52 float yMovement = 0.0f;
53 // Dispatch the moveX / MoveY events every frame.
54
55 xMovement = InputGamePadState.ThumbSticks.Left.X;
56 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
57
58 Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left));
59
60 if (InputKeyboardState.IsKeyDown(Keys.Left))
61 {
62 xMovement = -1.0f;
63 }
64
65 if (InputKeyboardState.IsKeyDown(Keys.Right))
66 {
67 xMovement = 1.0f;
68 }
69
70 if (InputKeyboardState.IsKeyDown(Keys.Up))
71 {
72 yMovement = -1.0f;
73 }
74
75 if (InputKeyboardState.IsKeyDown(Keys.Down))
76 {
77 yMovement = 1.0f;
78 }
79
80 Dispatch("moveX", xMovement);
81 Dispatch("moveY", yMovement);
82 }
83
84 public static void Bind(string eventName, Action<float> listener)
85 {
86 List<Action<float>> newListenerList;
87 List<Action<float>> listenerList;
88 bool foundListeners;
89
90 if (!Listeners.ContainsKey(eventName)) {
91 newListenerList = new List<Action<float>>();
92 Listeners.Add(eventName, newListenerList);
93 }
94
95 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
96
97 listenerList.Add(listener);
98 }
99
100 public static void Dispatch(string eventName, float value)
101 {
102 List<Action<float>> listenerList;
103 bool foundListeners;
104
105 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
106
107 if (!foundListeners)
108 {
109 return;
110 }
111
112 foreach (Action<float> method in listenerList)
113 {
114 method(value);
115 }
116 }
117 }
118 }